package com.qianfeng.biquge.controller;


import com.github.pagehelper.PageInfo;
import com.qianfeng.biquge.pojo.Authors;
import com.qianfeng.biquge.service.IAuthorService;
import com.qianfeng.biquge.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/author")
public class AuthorController {

    @Autowired
    private IAuthorService authorService;

    @PostMapping("/newauthor")
    public R addAuthor(@RequestBody Authors authors){

        authorService.addAuthor(authors);

        return R.setOk(authors);

    }

    @GetMapping("/authors/{pageNum}/{pageSize}")
    public R  getAllAuthor(@PathVariable int pageNum, @PathVariable int pageSize){

        PageInfo<Authors> pageInfo = authorService.getAllAuthor(pageNum, pageSize);

        return R.setOk(pageInfo);

    }

    @GetMapping("/author/{authorId}")
    public R getAuthorByAuthorId(@PathVariable Long authorId){

        Authors authors = authorService.getAuthorByAuthorId(authorId);

        return R.setOk(authors);

    }

    @GetMapping("/authors/{authorName}/{pageNum}/{pageSize}")
    public R getAuthorByAuthorName(@PathVariable String authorName,@PathVariable int pageNum,@PathVariable int pageSize) throws UnsupportedEncodingException {

        PageInfo<Authors> pageInfo = authorService.getAuthorByAuthorName(authorName, pageNum, pageSize);

        return R.setOk(pageInfo);

    }

    @PutMapping("/update")
    public R updateAuthor(@RequestBody Authors authors){

        int i = authorService.updateAuthor(authors);

        return R.setOk(authors);

    }

}
